package com.example.park2212.member

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.GlideUtils
import com.example.lib_base.mvvm.BaseActivity
import com.example.lib_base.net.Const
import com.example.park2212.R
import com.example.park2212.databinding.ActivityMemberBinding
import com.example.park2212.repair.RepairImgAdapter
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

@AndroidEntryPoint
@Route(path = "/app/member/MemberActivity")
class MemberActivity : BaseActivity<ActivityMemberBinding,MemberViewModel>() {

     lateinit var member:String


    override fun initView() {
        viewModel.getMember(6)

        binding.memberIv.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

        binding.memberSj.setOnClickListener {
           if(member == "黄金会员"){
               viewModel.upgradeMember(6,"铂金会员")
           }else if (member == "铂金会员"){
               viewModel.upgradeMember(6,"钻石会员")
           }else{
               ToastUtils.showLong("已经是最高级了")
           }
        }

    }

    override fun initData() {

        viewModel.upgradeMemberSuccess.observe(this){
            viewModel.getMember(6)
        }

        viewModel.getMemberSuccess.observe(this){

            member = it.membershipLevel
            ToastUtils.showLong("操作成功")
            binding.memberVip.setText(it.membershipLevel)
            binding.memberTime.setText(it.registrationDate)


            if (it.duration == 0 ){
                binding.memberTime.setText("还未办理会员")
            }else{
                val calendar = Calendar.getInstance()
                val split = it.registrationDate.split("-")
                calendar.set(split[0].toInt(),split[1].toInt(),split[2].toInt())
                calendar.add(Calendar.MONTH, it.duration-1)
                if(calendar.time.after(Date())){
                    binding.memberTime.setText("${viewModel.getDate(calendar.timeInMillis)} 到期")
                }else{
                    binding.memberTime.setText("已到期，请续费")
                }
            }
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK){
            val pictureBean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            GlideUtils.loadCircleCropImage(pictureBean?.path!!,binding.memberIv)
        }
    }

}